package com.nari.ncps.screen.service.impl.coal;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.entity.coal.CoalOpermonEquipUse;
import com.nari.ncps.screen.entity.coal.CoalProdoperOutput;
import com.nari.ncps.screen.mapper.coal.CoalProdoperOutputMapper;
import com.nari.ncps.screen.mapper.ne.NeProdoperElectricEffMapper;
import com.nari.ncps.screen.service.coal.CoalProdoperOutputService;
import com.nari.ncps.screen.vo.coal.CoalOutputTrendVO;
import com.nari.ncps.screen.vo.coal.CoalOutputUnitsVO;
import com.nari.ncps.screen.vo.coal.CoalOutputVO;
import com.nari.ncps.screen.vo.wholeproduct.CoalOutputCompletionVO;
import com.nari.ncps.screen.vo.wholeproduct.MonthlyOutputCompletionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 生产经营煤炭产量表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class CoalProdoperOutputServiceImpl extends ServiceImpl<CoalProdoperOutputMapper, CoalProdoperOutput> implements CoalProdoperOutputService {


    @Autowired
    private CoalProdoperOutputMapper coalProdoperOutputMapper;

    @Override
    public CoalOutputVO getCoalOutput(Integer year) {

        // 如果year为null，使用当前年份
        if (year == null) {
            year = LocalDate.now().getYear();
        }
        Map<String,BigDecimal> dataMap =new HashMap();
        dataMap=getCoalOutputLast(year-1);
        CoalOutputVO vo = new CoalOutputVO();
        BigDecimal zereo = BigDecimal.ZERO;
        LambdaQueryWrapper<CoalProdoperOutput> annualQuery = new LambdaQueryWrapper<>();
        annualQuery.eq(CoalProdoperOutput::getYear, year);
        annualQuery.eq(CoalProdoperOutput::getOrgCode, 100360);

        List<CoalProdoperOutput> annuallist = this.list(annualQuery);
        BigDecimal planTotal = BigDecimal.ZERO;
        BigDecimal actTotal = BigDecimal.ZERO;

        BigDecimal actTotalLast = BigDecimal.ZERO;//去年同期实际产量

        BigDecimal rateTotal = BigDecimal.ZERO;
        BigDecimal yoyTotal = BigDecimal.ZERO;
        if(!annuallist.isEmpty()) {
            planTotal =
                    annuallist.stream().map(CoalProdoperOutput::getAnnualPlan).filter(Objects::nonNull).reduce(BigDecimal.ZERO,
                            BigDecimal::add).setScale(2, RoundingMode.HALF_UP);

            actTotal = annuallist.stream().map(CoalProdoperOutput::getAnnualActual).filter(Objects::nonNull).reduce(BigDecimal.ZERO,
                    BigDecimal::add).setScale(2, RoundingMode.HALF_UP);
            rateTotal = annuallist.stream().map(CoalProdoperOutput::getAnnualCompletionRate).filter(Objects::nonNull).reduce(BigDecimal.ZERO,
                    BigDecimal::add).setScale(2, RoundingMode.HALF_UP);
            yoyTotal = annuallist.stream().map(CoalProdoperOutput::getAnnualYoyRate).filter(Objects::nonNull).reduce(BigDecimal.ZERO,
                    BigDecimal::add).setScale(2, RoundingMode.HALF_UP);
        }

        actTotalLast=dataMap.get("actTotalLast");
        if(zereo.equals(actTotalLast)){
            yoyTotal=zereo;
        }
        else {
            yoyTotal = actTotal.subtract(actTotalLast).divide(actTotalLast, 2, RoundingMode.HALF_UP);//年同比
        }
        vo.setAnnualPlan(planTotal);//年计划完成量
        vo.setAnnualActual(actTotal);//年实际完成量
        // vo.setAnnualCompletionRate(rateTotal);
        vo.setAnnualCompletionRate(actTotal.divide(planTotal, 2, RoundingMode.HALF_UP));//年完成率
        vo.setAnnualYoyRate(yoyTotal);

        LambdaQueryWrapper<CoalProdoperOutput> monthQuery = new LambdaQueryWrapper<>();
        monthQuery.eq(CoalProdoperOutput::getDateType, 1)
                .eq(CoalProdoperOutput::getYear, year)
                .eq(CoalProdoperOutput::getOrgCode, 100360)
                .orderByDesc(CoalProdoperOutput::getMonth).last("LIMIT 1");

        List<CoalProdoperOutput> monthlist = this.list(monthQuery);

        BigDecimal mplanTotal = BigDecimal.ZERO;
        BigDecimal mactTotal = BigDecimal.ZERO;
        BigDecimal mrateTotal = BigDecimal.ZERO;
        BigDecimal myoyTotal = BigDecimal.ZERO;
        BigDecimal mactTotalLast = BigDecimal.ZERO;//去年同期月实际产量
        if(!monthlist.isEmpty()) {
//            mplanTotal = monthlist.stream().map(CoalProdoperOutput::getMonthlyPlan).filter(Objects::nonNull).reduce(BigDecimal.ZERO,
//                    BigDecimal::add).setScale(2, RoundingMode.HALF_UP);
//
//            mactTotal = monthlist.stream().map(CoalProdoperOutput::getMonthlyActual).filter(Objects::nonNull).reduce(BigDecimal.ZERO,
//                    BigDecimal::add).setScale(2, RoundingMode.HALF_UP);
//            mrateTotal = monthlist.stream().map(CoalProdoperOutput::getMonthlyCompletionRate).filter(Objects::nonNull).reduce(BigDecimal.ZERO,
//                    BigDecimal::add).setScale(2, RoundingMode.HALF_UP);
//            myoyTotal = monthlist.stream().map(CoalProdoperOutput::getMonthlyYoyRate).filter(Objects::nonNull).reduce(BigDecimal.ZERO,
//                    BigDecimal::add).setScale(2, RoundingMode.HALF_UP);
            mplanTotal = monthlist.get(0).getMonthlyPlan();
            mactTotal = monthlist.get(0).getMonthlyActual();
            mrateTotal = monthlist.get(0).getMonthlyCompletionRate();
//            myoyTotal = monthlist.get(0).getMonthlyYoyRate();
        }
        mactTotalLast=dataMap.get("mactTotalLast");//去年为零
        vo.setMonthlyPlan(mplanTotal);//月计划完成量
        vo.setMonthlyActual(mactTotal);//月实际完成量
        //vo.setMonthlyCompletionRate(mrateTotal);
//        if(mplanTotal!=null){
//            vo.setMonthlyCompletionRate(mactTotal.divide(mplanTotal, 2, RoundingMode.DOWN));//月完成率
//        }else {
//            vo.setMonthlyCompletionRate(monthlist.get(0).getMonthlyCompletionRate());
//        }

        if (mplanTotal != null && mactTotal != null && mplanTotal.compareTo(BigDecimal.ZERO) != 0) {
            // 两个值都不为null，且除数不为0
            BigDecimal rate = mactTotal.divide(mplanTotal, 2, RoundingMode.DOWN);
            vo.setMonthlyCompletionRate(rate);
        } else if (monthlist != null && !monthlist.isEmpty() && monthlist.get(0) != null) {
            // 使用备用值
            vo.setMonthlyCompletionRate(monthlist.get(0).getMonthlyCompletionRate());
        } else {
            // 所有备用方案都失败，设置默认值
            vo.setMonthlyCompletionRate(BigDecimal.ZERO);
        }

        if(zereo.equals(mactTotalLast)){
            vo.setMonthlyYoyRate(zereo);
        }
        else {
            myoyTotal = mactTotal.subtract(mactTotalLast).divide(mactTotalLast, 2, RoundingMode.HALF_UP);//年同比
        }
        vo.setMonthlyYoyRate(myoyTotal);

        return vo;
    }

    @Override
    public List<CoalOutputUnitsVO> getCoalOutputByUnits(Integer year) {
        // 如果year为null，使用当前年份
        if (year == null) {
            year = LocalDate.now().getYear();
        }

        // 查询各单位年度数据（date_type=0表示年度数据, org_code不为TOTAL表示非汇总数据）
        LambdaQueryWrapper<CoalProdoperOutput> query = new LambdaQueryWrapper<>();
        query.eq(CoalProdoperOutput::getYear, year)
             .eq(CoalProdoperOutput::getDateType, 0)  // 0表示年度数据
//           .ne(CoalProdoperOutput::getOrgCode, "TOTAL")  // 排除汇总数据
             .like(CoalProdoperOutput::getOrgCode, "50000%")  // 排除汇总数据
             .orderByAsc(CoalProdoperOutput::getOrgCode);

        List<CoalProdoperOutput> dataList = this.list(query);

        // 创建返回结果列表
        List<CoalOutputUnitsVO> resultList = new ArrayList<>();

        // 如果数据库为空，直接返回空列表
        if (dataList == null || dataList.isEmpty()) {
            return resultList;
        }

        // 转换为VO列表
        BigDecimal divisor = new BigDecimal("1");  // 将吨转换为万吨

        for (CoalProdoperOutput data : dataList) {
            CoalOutputUnitsVO vo = new CoalOutputUnitsVO();
//            50000006	内蒙古扎鲁特旗扎哈淖尔煤业
//            50000007	内蒙古北露天煤矿
//            50000008	内蒙古白音华二号矿
//            50000005	内蒙古白音华三号矿
//            50000004	内蒙古南露天煤矿

            // 设置单位信息
            if("50000004".equals(data.getOrgCode())){
                vo.setUnitName("南矿");
            }else if("50000005".equals(data.getOrgCode())){
                vo.setUnitName("三号矿");
            }else if("50000006".equals(data.getOrgCode())){
                vo.setUnitName("扎矿");
            }else if("50000007".equals(data.getOrgCode())){
                vo.setUnitName("北矿");
            }else if("50000008".equals(data.getOrgCode())){
                vo.setUnitName("二号矿");
            }
//            vo.setUnitName(data.getOrgName());
            vo.setUnitCode(data.getOrgCode());

            // 设置年计划完成情况（万吨）
            if (data.getAnnualPlan() != null) {
                vo.setAnnualPlan(data.getAnnualPlan().divide(divisor, 2, RoundingMode.HALF_UP));
            } else {
                vo.setAnnualPlan(BigDecimal.ZERO);
            }

            // 设置年当前完成情况（万吨）
            if (data.getAnnualActual() != null) {
                vo.setAnnualActual(data.getAnnualActual().divide(divisor, 2, RoundingMode.HALF_UP));
            } else {
                vo.setAnnualActual(BigDecimal.ZERO);
            }

            resultList.add(vo);
        }

        return resultList;
    }

    @Override
    public List<CoalOutputTrendVO> getCoalOutputTrend(Integer year) {
        // 如果year为null，使用当前年份
        int currentYear = year != null ? year : LocalDate.now().getYear();
        
        // 计算最近5年的年份范围（包含当前年）
        // 例如：当前年是2025，则查询2021-2025
        int startYear = currentYear - 4;
        
        // 查询最近5年的年度数据（date_type=0表示年度数据）
        // 需要查询所有org_code的数据进行汇总
        LambdaQueryWrapper<CoalProdoperOutput> query = new LambdaQueryWrapper<>();
        query.eq(CoalProdoperOutput::getDateType, 0)  // 0表示年度数据
             .eq(CoalProdoperOutput::getOrgCode, 100360)
             .ge(CoalProdoperOutput::getYear, startYear)  // 年份 >= startYear
             .le(CoalProdoperOutput::getYear, currentYear)  // 年份 <= currentYear
             .orderByAsc(CoalProdoperOutput::getYear);
        
        List<CoalProdoperOutput> dataList = this.list(query);
        
        // 创建返回结果列表
        List<CoalOutputTrendVO> resultList = new ArrayList<>();
        
        // 如果数据库为空，直接返回空列表
        if (dataList == null || dataList.isEmpty()) {
            return resultList;
        }
        
        // 按年份分组汇总数据
        // 使用Map来存储每个年份的汇总数据
        java.util.Map<Integer, CoalOutputTrendVO> yearDataMap = new java.util.LinkedHashMap<>();
        
        for (CoalProdoperOutput data : dataList) {
            Integer dataYear = data.getYear();
            if (dataYear == null) {
                continue;
            }
            
            // 获取或创建该年份的VO对象
            CoalOutputTrendVO vo = yearDataMap.computeIfAbsent(dataYear, k -> {
                CoalOutputTrendVO newVo = new CoalOutputTrendVO();
                newVo.setYear(k);
                newVo.setAnnualPlan(BigDecimal.ZERO);
                newVo.setAnnualActual(BigDecimal.ZERO);
                return newVo;
            });
            
            // 汇总年计划完成情况
            if (data.getAnnualPlan() != null) {
                vo.setAnnualPlan(vo.getAnnualPlan().add(data.getAnnualPlan()));
            }
            
            // 汇总年当前完成情况
            if (data.getAnnualActual() != null) {
                vo.setAnnualActual(vo.getAnnualActual().add(data.getAnnualActual()));
            }
        }
        
        // 计算完成率并转换为列表
        for (CoalOutputTrendVO vo : yearDataMap.values()) {
            // 计算年当前完成率（已完成/计划 * 100）
            if (vo.getAnnualPlan() != null && vo.getAnnualPlan().compareTo(BigDecimal.ZERO) > 0) {
                BigDecimal completionRate = vo.getAnnualActual()
                        .divide(vo.getAnnualPlan(), 4, RoundingMode.HALF_UP)
                        .multiply(new BigDecimal("100"))
                        .setScale(2, RoundingMode.HALF_UP);
                vo.setAnnualCompletionRate(completionRate);
            } else {
                vo.setAnnualCompletionRate(BigDecimal.ZERO);
            }
            
            resultList.add(vo);
        }
        
        return resultList;
    }

    @Override
    public CoalOutputCompletionVO getCoalOutputCompletion(Integer year) {
        // 如果year为null，使用当前年份
        if (year == null) {
            year = LocalDate.now().getYear();
        }

        // 获取当前月份
        //TODO 目前数据不全导致查询条件为当前月的前一个月
        int currentMonth = LocalDate.now().getMonthValue()-1;

        // 创建返回对象
        CoalOutputCompletionVO vo = new CoalOutputCompletionVO();

        // 查询年度数据（year_month_type=0, unit_code='TOTAL'）
        LambdaQueryWrapper<CoalProdoperOutput> annualQuery = new LambdaQueryWrapper<>();
        annualQuery.eq(CoalProdoperOutput::getYear, year)
                .eq(CoalProdoperOutput::getDateType, 0)
//                   .eq(CoalProdoperOutput::getYearMonthType, false)  // 0表示年度数据
                .eq(CoalProdoperOutput::getOrgCode, "100360");
//        CoalProdoperOutput annualData = this.getOne(annualQuery);
        CoalProdoperOutput annualData = this.list(annualQuery).stream().findFirst().orElse(null);

        if (annualData != null) {
            // 将吨转换为万吨
            BigDecimal divisor = new BigDecimal("10000");

            if (annualData.getAnnualPlan() != null) {
                vo.setAnnualPlan(annualData.getAnnualPlan());
//                vo.setAnnualPlan(annualData.getAnnualPlan().divide(divisor, 2, RoundingMode.HALF_UP));
            }

            if (annualData.getAnnualActual() != null) {
                vo.setAnnualActual(annualData.getAnnualActual());
//                vo.setAnnualActual(annualData.getAnnualActual().divide(divisor, 2, RoundingMode.HALF_UP));
            }

            if (annualData.getAnnualYoyRate() != null) {
                vo.setAnnualYoyRate(annualData.getAnnualYoyRate());
            }
        }

        // 查询月度数据（year_month_type=1, unit_code='TOTAL', month=当前月）
        LambdaQueryWrapper<CoalProdoperOutput> monthlyQuery = new LambdaQueryWrapper<>();
        monthlyQuery.eq(CoalProdoperOutput::getYear, year)
                .eq(CoalProdoperOutput::getMonth, currentMonth)
                .eq(CoalProdoperOutput::getDateType, 1)
//                    .eq(CoalProdoperOutput::getYearMonthType, true)  // 1表示月度数据
                .eq(CoalProdoperOutput::getOrgCode, "100360");
//        CoalProdoperOutput monthlyData = this.getOne(monthlyQuery);
        CoalProdoperOutput monthlyData = this.list(monthlyQuery).stream().findFirst().orElse(null);

        if (monthlyData != null) {
            // 将吨转换为万吨
            BigDecimal divisor = new BigDecimal("10000");

            if (monthlyData.getMonthlyPlan() != null) {
                vo.setMonthlyPlan(monthlyData.getMonthlyPlan());
//                vo.setMonthlyPlan(monthlyData.getMonthlyPlan().divide(divisor, 2, RoundingMode.HALF_UP));
            }

            if (monthlyData.getMonthlyActual() != null) {
                vo.setMonthlyActual(monthlyData.getMonthlyActual());
//                vo.setMonthlyActual(monthlyData.getMonthlyActual().divide(divisor, 2, RoundingMode.HALF_UP));
            }

            if (monthlyData.getMonthlyYoyRate() != null) {
                vo.setMonthlyYoyRate(monthlyData.getMonthlyYoyRate());
            }
        }

        return vo;
    }


    //11.3 日修改 张右升
    @Override
    public List<MonthlyOutputCompletionVO> getMonthlyOutputCompletion(Integer year) {


        LocalDate now = LocalDate.now();
        YearMonth currentYearMonth = YearMonth.from(now);
        List<CoalProdoperOutput> dataList=new ArrayList<>();

        // 计算最近12个月的起始年月
        YearMonth startYearMonth = currentYearMonth.minusMonths(11);
        for (int i = 0; i < 12; i++) {
            YearMonth ym = startYearMonth.plusMonths(i);
            ;

            CoalProdoperOutput coalProdoperOutput= coalProdoperOutputMapper.getMonthlyOutputCompletion(String.valueOf(ym.getYear()),String.valueOf(ym.getMonthValue()));
            if(coalProdoperOutput==null){
                CoalProdoperOutput coalProdoperOutput1=new CoalProdoperOutput();
                coalProdoperOutput1.setYear(ym.getYear());
                coalProdoperOutput1.setMonth(ym.getMonthValue());
                dataList.add(coalProdoperOutput1);
            }
            else{
                coalProdoperOutput.setYear(ym.getYear());
                coalProdoperOutput.setMonth(ym.getMonthValue());
                dataList.add(coalProdoperOutput);
            }


        }

        // 转换为VO列表
        List<MonthlyOutputCompletionVO> resultList = new ArrayList<>();
        BigDecimal divisor = new BigDecimal("10000");  // 将吨转换为万吨

        for (CoalProdoperOutput data : dataList) {
            MonthlyOutputCompletionVO vo = new MonthlyOutputCompletionVO();
            vo.setYear(data.getYear());
            vo.setMonth(data.getMonth());

            if (data.getMonthlyPlan() != null) {
                // vo.setMonthlyPlan(data.getMonthlyPlan().divide(divisor, 2, RoundingMode.HALF_UP));
                vo.setMonthlyPlan(data.getMonthlyPlan());
            }

            if (data.getMonthlyActual() != null) {
               // vo.setMonthlyActual(data.getMonthlyActual().divide(divisor, 2, RoundingMode.HALF_UP));
                vo.setMonthlyActual(data.getMonthlyActual().setScale(2, RoundingMode.HALF_UP));
            }

//            if (data.getMonthlyYoyRate() != null) {
//                vo.setMonthlyYoyRate(data.getMonthlyYoyRate().setScale(2, RoundingMode.HALF_UP));
//            }

            if (data.getMonthlyActual() != null && data.getMonthlyPlan() != null) {
                vo.setMonthlyYoyRate(data.getMonthlyActual().divide(data.getMonthlyPlan(),2, RoundingMode.HALF_UP));
            }



            resultList.add(vo);
        }

        return resultList;

    }


    public List<MonthlyOutputCompletionVO> getMonthlyOutputCompletionOld(Integer year) {
        // 如果year为null，使用当前年月
        LocalDate now = LocalDate.now();
        YearMonth currentYearMonth = YearMonth.from(now);
        
        // 计算最近12个月的起始年月
        YearMonth startYearMonth = currentYearMonth.minusMonths(11);
        
        // 查询最近12个月的月度数据（year_month_type=1, unit_code='TOTAL'）
        LambdaQueryWrapper<CoalProdoperOutput> query = new LambdaQueryWrapper<>();
        query
//                .eq(CoalProdoperOutput::getYearMonthType, true)  // 1表示月度数据
             .eq(CoalProdoperOutput::getOrgCode, "TOTAL")
             .and(wrapper -> {
                 // 构建年月范围查询
                 for (int i = 0; i < 12; i++) {
                     YearMonth ym = startYearMonth.plusMonths(i);
                     if (i == 0) {
                         wrapper.and(w -> w.eq(CoalProdoperOutput::getYear, ym.getYear())
                                          .eq(CoalProdoperOutput::getMonth, ym.getMonthValue()));
                     } else {
                         wrapper.or(w -> w.eq(CoalProdoperOutput::getYear, ym.getYear())
                                         .eq(CoalProdoperOutput::getMonth, ym.getMonthValue()));
                     }
                 }
             })
             .orderByDesc(CoalProdoperOutput::getYear)
             .orderByDesc(CoalProdoperOutput::getMonth);
        
        List<CoalProdoperOutput> dataList = this.list(query);
        
        // 转换为VO列表
        List<MonthlyOutputCompletionVO> resultList = new ArrayList<>();
        BigDecimal divisor = new BigDecimal("10000");  // 将吨转换为万吨
        
        for (CoalProdoperOutput data : dataList) {
            MonthlyOutputCompletionVO vo = new MonthlyOutputCompletionVO();
            vo.setYear(data.getYear());
            vo.setMonth(data.getMonth());
            
            if (data.getMonthlyPlan() != null) {
                vo.setMonthlyPlan(data.getMonthlyPlan().divide(divisor, 2, RoundingMode.HALF_UP));
            }
            
            if (data.getMonthlyActual() != null) {
                vo.setMonthlyActual(data.getMonthlyActual().divide(divisor, 2, RoundingMode.HALF_UP));
            }
            
            if (data.getMonthlyYoyRate() != null) {
                vo.setMonthlyYoyRate(data.getMonthlyYoyRate());
            }
            
            resultList.add(vo);
        }
        
        return resultList;
    }


    @Override
    public List<MonthlyOutputCompletionVO> getCoalMonthlyOutputCompletion(Integer year) {
        List<MonthlyOutputCompletionVO> list =new ArrayList<>();

        // 查询年度数据（year_month_type=0, unit_code='TOTAL'）
        LambdaQueryWrapper<CoalProdoperOutput> annualQuery = new LambdaQueryWrapper<>();
        annualQuery.eq(CoalProdoperOutput::getDateType, 1)
                .eq(CoalProdoperOutput::getOrgCode, "100360")
                .orderByAsc(CoalProdoperOutput::getBid);
        List<CoalProdoperOutput> dataList = this.list(annualQuery);
        for(CoalProdoperOutput data:dataList){
            // 创建返回对象
            MonthlyOutputCompletionVO vo = new MonthlyOutputCompletionVO();
            vo.setYear(data.getYear());
            vo.setMonth(data.getMonth());
            vo.setMonthlyPlan(data.getMonthlyPlan());
            vo.setMonthlyActual(data.getMonthlyActual());
            vo.setMonthlyYoyRate(data.getMonthlyYoyRate());
            list.add(vo);
        }
//        list.sort(Comparator.comparingInt(MonthlyOutputCompletionVO::getMonth));
        return list;
    }



    //获取去年当前时间完成量和去年当前月完成量
    public Map<String,BigDecimal> getCoalOutputLast(Integer year) {
        Map<String,BigDecimal> dataMap =new HashMap();
        LambdaQueryWrapper<CoalProdoperOutput> annualQuery = new LambdaQueryWrapper<>();
        annualQuery.eq(CoalProdoperOutput::getDateType, 0);
        List<CoalProdoperOutput> annuallist = this.list(annualQuery);
        BigDecimal actTotalLast = BigDecimal.ZERO;//去年同期实际产量
        if(!annuallist.isEmpty()) {

            actTotalLast = annuallist.stream().map(CoalProdoperOutput::getAnnualActual).filter(Objects::nonNull).reduce(BigDecimal.ZERO,
                    BigDecimal::add);

        }
        dataMap.put("actTotalLast",actTotalLast);
        LambdaQueryWrapper<CoalProdoperOutput> monthQuery = new LambdaQueryWrapper<>();
        monthQuery.eq(CoalProdoperOutput::getDateType, 1)
                .eq(CoalProdoperOutput::getYear, year);

        List<CoalProdoperOutput> monthlist = this.list(monthQuery);
        BigDecimal mactTotalLast = BigDecimal.ZERO;
        if(!monthlist.isEmpty()) {
            mactTotalLast = monthlist.stream().map(CoalProdoperOutput::getMonthlyActual).filter(Objects::nonNull).reduce(BigDecimal.ZERO,
                    BigDecimal::add);

        }
        dataMap.put("mactTotalLast",mactTotalLast);

        return dataMap;

    }



}
